
   package snuvm.mips.instructions.syscalls;
	

   import javax.sound.midi.*;

/*
Copyright (c) 2003-2007,  Pete Sanderson and Kenneth Vollmar

Developed by Pete Sanderson (psanderson"at"otterbein.edu)
and Kenneth Vollmar (kenvollmar"at"missouristate.edu)

Permission is hereby granted, free of charge, to any person obtaining 
a copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject 
to the following conditions:

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

(MIT license, http://www.opensource.org/licenses/mit-license.html)
 */

	
  /////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////
  //
  //  The ToneGenerator and Tone classes were developed by Otterbein College
  //  student Tony Brock in July 2007.  They simulate MIDI output through the
  //  computers soundcard using classes and methods of the javax.sound.midi
  //  package.  I will not provide the default parameter versions to
  //  the MIPS programmer, who will have to specify values for all four
  //  parameters.
  //
  /////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////
  
  
   /*
    * Creates a Tone object and passes it to a thread to "play" it using MIDI.
    */
    class ToneGenerator {
      
      /**
      * The default pitch value for the tone: 60 / middle C.
      */    
      public final static byte DEFAULT_PITCH = 60;
      
      /**
      * The default duration of the tone: 1000 milliseconds.
      */    
      public final static int DEFAULT_DURATION = 1000;
      
      /**
      * The default instrument of the tone: 0 / piano.
      */          
      public final static byte DEFAULT_INSTRUMENT = 0;
      
      /**
      * The default volume of the tone: 100 (of 127).
      */          
      public final static byte DEFAULT_VOLUME = 100;
      
      /**
      * Produces a Tone with all default settings - pitch at middle C,
      * 1000 milliseconds in duration, instrument set to piano, at a
      * volume of 100 (out of 127).
      *
      * @return the thread responsible for playing the Tone.
      */       
       public Thread generateTone() {
         return generateTone(DEFAULT_PITCH, DEFAULT_DURATION,
               	DEFAULT_INSTRUMENT, DEFAULT_VOLUME);
      }
      
      /**
      * Produces a Tone with the specified pitch.  The other variables
      * are given default settings - 1000 milliseconds in duration,
      * instrument set to piano, at a volume of 100 (of 127).
      *
      * @param pitch the desired pitch in semitones - 0-127 where 60 is 
      * middle C.
      * @return the thread responsible for playing the Tone.
      */ 
       public Thread generateTone(byte pitch) {
         return generateTone (pitch, DEFAULT_DURATION,
               				  DEFAULT_INSTRUMENT, DEFAULT_VOLUME);
      }
      
      /**
      * Produces a Tone with the specified duration.  The other variables
      * are given default settings - pitch at middle C, instrument set 
      * to piano, at a volume of 100 (of 127).
      *
      * @param duration the desired duration in milliseconds.
      * @return the thread responsible for playing the Tone.
      */    	
       public Thread generateTone(int duration) {
         return generateTone (DEFAULT_PITCH, duration,
               				  DEFAULT_INSTRUMENT, DEFAULT_VOLUME);
      }
      
      /**
      * Produces a Tone with the specified pitch and duration.  The other 
      * variables are given default settings - instrument set to piano 
      * at a volume of 100 (of 127).
      *
      * @param pitch the desired pitch in semitones - 0-127 where 60 is 
      * middle C.
      * @param duration the desired duration in milliseconds.
      * @return the thread responsible for playing the Tone.
      */    	   	
       public Thread generateTone(byte pitch, int duration) {
         return generateTone (pitch, duration, DEFAULT_INSTRUMENT,
               				  DEFAULT_VOLUME);
      }
      
      /**
      * Produces a Tone with the specified pitch, duration, and instrument.
      * Volume is set to 100 (of 127).
      *
      * @param pitch the desired pitch in semitones - 0-127 where 60 is 
      * middle C.
      * @param duration the desired duration in milliseconds.
      * @param instrument the desired instrument (or patch) represented
      * by a positive byte value (0-127).  See the <a href=
      * http://www.midi.org/about-midi/gm/gm1sound.shtml#instrument>general 
      * MIDI instrument patch map</a> for more instruments associated with
      * each value.
      * @return the thread responsible for playing the Tone.
      */
       public Thread generateTone(byte pitch, int duration, byte instrument) {
         return generateTone (pitch, duration, instrument,
               				  DEFAULT_VOLUME);
      }   
      
      /**
      * Produces a Tone with the specified pitch, duration, and instrument,
      * and volume.
      *
      * @param pitch the desired pitch in semitones - 0-127 where 60 is 
      * middle C.
      * @param duration the desired duration in milliseconds.
      * @param instrument the desired instrument (or patch) represented
      * by a positive byte value (0-127).  See the <a href=
      * http://www.midi.org/about-midi/gm/gm1sound.shtml#instrument>general 
      * MIDI instrument patch map</a> for more instruments associated with
      * each value.
      * @param volume the desired volume of the initial attack of the 
      * Tone (MIDI velocity) represented by a positive byte value (0-127).
      * @return the thread responsible for playing the Tone.
      */   
       public Thread generateTone(byte pitch, int duration,
          								byte instrument, byte volume) {
         Runnable tone = new Tone(pitch, duration, instrument, volume);
         Thread t = new Thread(tone);
         t.start();
         return t;
      }
      
   }
   
   
   /**
    * Contains important variables for a MIDI Tone: pitch, duration
    * instrument (patch), and volume.  The tone can be passed to a thread
    * and will be played using MIDI.
    */
    class Tone implements Runnable {
      
      /**
      * Tempo of the tone is in milliseconds: 1000 beats per second.
      */
      
      public final static int TEMPO = 1000;
      /**
      * The default MIDI channel of the tone: 0 (channel 1).
      */      
      public final static int DEFAULT_CHANNEL = 0;
      
      private byte pitch;
      private int duration;
      private byte instrument;
      private byte volume;
      
      /**
      * Instantiates a new Tone object, initializing the tone's pitch,
      * duration, instrument (patch), and volume.
      *
      * @param pitch the pitch in semitones.  Pitch is represented by 
      * a positive byte value - 0-127 where 60 is middle C.
      * @param duration the duration of the tone in milliseconds.
      * @param instrument a positive byte value (0-127) which represents
      * the instrument (or patch) of the tone.  See the <a href=
      * http://www.midi.org/about-midi/gm/gm1sound.shtml#instrument>general 
      * MIDI instrument patch map</a> for more instruments associated with
      * each value.
      * @param volume a positive byte value (0-127) which represents the
      * volume of the initial attack of the note (MIDI velocity).  127 being
      * loud, and 0 being silent.
      */ 
       public Tone(byte pitch, int duration, byte instrument, byte volume) {
         this.pitch = pitch;
         this.duration = duration;
         this.instrument = instrument;
         this.volume = volume;
      }
      
      /**
      * Plays the tone.
      */
       public void run() {
         playTone();
      }
      
       private void playTone() {
         
         try {
            Sequencer player = MidiSystem.getSequencer();
            player.open();
            
            Sequence seq = new Sequence(Sequence.PPQ, 1);
            player.setTempoInMPQ(TEMPO);
            Track	t = seq.createTrack();
            
            //select instrument
            ShortMessage inst = new ShortMessage();
            inst.setMessage(ShortMessage.PROGRAM_CHANGE, DEFAULT_CHANNEL, instrument, 0);
            MidiEvent instChange = new MidiEvent(inst, 0);
            t.add(instChange);
              
            ShortMessage on = new ShortMessage();
            on.setMessage(ShortMessage.NOTE_ON, DEFAULT_CHANNEL, pitch, volume);
            MidiEvent noteOn = new MidiEvent(on, 0);
            t.add(noteOn);
               
            ShortMessage off = new ShortMessage();
            off.setMessage(ShortMessage.NOTE_OFF, DEFAULT_CHANNEL, pitch, volume);
            MidiEvent noteOff = new MidiEvent(off, duration);
            t.add(noteOff);
            
            player.setSequence(seq);
            
            player.start();
            
            try {
               Thread.sleep(duration);
            } 
                catch (InterruptedException ex) {
               }
            finally {
               player.close();
            }
            
         } 
             catch (MidiUnavailableException mue) {
               mue.printStackTrace();   
            } 
             catch (InvalidMidiDataException imde) {
               imde.printStackTrace();
            }
      }
   }
